package com.noname.lib_base_java.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Author: Lmoumou
 * Date: 2018-09-21 10:08
 * 文件名: ViewHolder
 * 描述:
 */
public class ViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> mView;

    public ViewHolder(View itemView) {
        super(itemView);
        mView = new SparseArray<>();
    }

    public <T extends View> T getView(int viewId) {
        View view = mView.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            mView.put(viewId, view);
        }
        return (T) view;
    }

    public <T extends ViewGroup> T getViewGroup(int vieeId) {
        View view = mView.get(vieeId);
        if (view == null) {
            view = itemView.findViewById(vieeId);
            mView.put(vieeId, view);
        }
        return (T) view;
    }

    //封装通用功能
    public ViewHolder setText(int viewId, CharSequence text) {
        TextView view = getView(viewId);
        view.setText(String.valueOf(text));
        return this;
    }

    public ViewHolder setHintText(int viewId, CharSequence text) {
        TextView view = getView(viewId);
        view.setHint(String.valueOf(text));
        return this;
    }

    public ViewHolder setImageResource(int viewId, int resId) {
        ImageView view = getView(viewId);
        view.setImageResource(resId);
        return this;
    }


}
